package com.ants.boot.system.controller;

import com.ants.boot.core.Response;
import com.ants.boot.core.annotation.Log;
import com.ants.boot.core.exception.UploadFileException;
import com.ants.boot.core.security.jwt.JwtTokenProvider;
import com.ants.boot.core.security.utils.SecurityUtils;
import com.ants.boot.model.BaseController;
import com.ants.boot.system.domain.SysMenu;
import com.ants.boot.system.domain.SysUser;
import com.ants.boot.system.dto.sysrole.UpdateStatusDto;
import com.ants.boot.system.dto.sysuser.*;
import com.ants.boot.system.service.SysMenuService;
import com.ants.boot.system.service.SysUserRoleService;
import com.ants.boot.system.service.SysUserService;
import com.ants.boot.system.vo.sysuser.UserProfileVo;
import com.ants.boot.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 蚂蚁会花呗
 * @date 2021/11/26 15:05
 */
@RestController
@RequestMapping("/system/sys_user")
@Api(tags = "用户管理")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private JwtTokenProvider jwtTokenProvider;

    @Autowired
    private SysMenuService sysMenuService;

    @Resource
    private RedisUtils redisUtils;

    @Autowired
    private SysUserRoleService sysUserRoleService;


    /**
     * 获取登录的用户信息，角色，权限信息
     * @param request
     * @return
     */
    @GetMapping("get_user_info")
    @Log(module = "用户信息",operate = "获取用户信息")
    @ApiOperation(value="用户信息",notes="获取用户信息")
    public Response getUserInfo(HttpServletRequest request){
        return Response.okData(sysUserService.getUserInfo(super.getUserId(request,jwtTokenProvider)));
    }

    /**
     * 动态获取路由
     * @return
     */
    @GetMapping("get_router_menus")
    @Log(module = "路由信息",operate = "获取菜单路由")
    @ApiOperation(value = "路由信息",notes = "左侧动态菜单路由")
    public Response getRouterMenus(){
        List<SysMenu>  menus = sysMenuService.findMenuByUserId(SecurityUtils.getLoginUser().getUserId());
        return Response.okData(sysMenuService.buildRouterVoMenus(menus));
    }

    /**
     * 获取用户对应的角色id列表
     * @param dto
     * @return
     */
    @PostMapping("list_role_by_user_id")
    @ApiOperation(value = "获取用户角色信息",notes = "获取用户对应的角色id列表")
    public Response listRoleByUserId(@RequestBody @Validated UserIdDto dto){
        List<Integer> list = sysUserRoleService.listRoleByUserId(dto.getUserId());
        return Response.okData(list);
    }


    /**
     * 搜索查询用户信息
     * @param dto
     * @return
     */
    @PostMapping("page_info")
    @Log(module = "用户信息",operate = "查询、搜索")
    @ApiOperation(value = "用户信息列表",notes = "搜素查询用户信息")
    public Response pageInfo(@RequestBody @Validated QueryUserDto dto){
        return Response.okData(sysUserService.pageInfo(dto));
    }

    /**
     * 新增用户
     * @param dto
     * @return
     */
    @PostMapping("add_user")
    @Log(module = "用户信息",operate = "新增用户信息")
    @ApiOperation(value = "新增用户信息",notes = "新增用户信息")
    public Response addUser(@RequestBody @Validated AddUserDto dto){
        SysUser result = sysUserService.addUser(dto);
        if (result == null){
            return Response.fail();
        }
        return Response.ok();
    }

    /**
     * 更新用户
     * @param dto
     * @return
     */
    @PostMapping("update_user")
    @Log(module = "用户信息",operate = "更新用户信息")
    @ApiOperation(value = "更新用户信息",notes = "更新用户信息")
    public Response updateUser(@RequestBody @Validated UpdateUserDto dto){
        boolean result = sysUserService.updateUser(dto);
        if (!result){
            return Response.fail();
        }
        return Response.ok();
    }

    /**
     * 删除用户信息
     * @param dto
     * @return
     */
    @PostMapping("del_user")
    @Log(module = "用户信息",operate = "删除用户信息")
    @ApiOperation(value = "删除用户信息",notes = "删除用户信息")
    public Response delUser(@RequestBody @Validated DelUserDto dto){
        sysUserService.delUser(dto.getIds());
        return Response.ok();
    }

    /**
     * 更新状态
     * @param dto
     * @return
     */
    @PostMapping("update_status")
    @Log(module = "用户信息",operate = "修改状态")
    @ApiOperation(value = "修改状态",notes = "修改状态")
    public Response updateStatus(@RequestBody @Validated UpdateStatusDto dto){
        boolean result = sysUserService.updateStatus(dto.getId(),dto.getStatus());
        if (!result){
            return Response.fail();
        }
        return Response.ok();
    }

    /**
     * 重置密码
     * @return
     */
    @PostMapping("reset_password")
    @Log(module = "用户信息",operate = "重置密码")
    @ApiOperation(value = "重置密码",notes = "重置密码")
    public Response resetPassword(@RequestBody @Validated ResetPasswordDto dto){
        boolean result = sysUserService.resetPassword(dto);
        if (!result){
            return Response.fail();
        }
        return Response.ok();
    }

    /**
     *
     * @return
     */
    @GetMapping("profile")
    @Log(module = "用户信息",operate = "获取个人中心数据")
    @ApiOperation(value = "个人中心详情",notes = "获取个人中心数据")
    public Response profile(){
        UserProfileVo vo = new UserProfileVo();
        SysUser sysUser = sysUserService.getById(SecurityUtils.getLoginUser().getUserId());
        BeanUtils.copyProperties(sysUser,vo);
        return Response.okData(vo);
    }

    /**
     * 更新头像
     * @return
     */
    @PostMapping("upload_avatar")
    @Log(module = "用户信息",operate = "上传头像")
    @ApiOperation(value = "上传头像",notes = "上传头像")
    public Response uploadAvatar(@RequestParam("file") MultipartFile file){
        String avatarPath = sysUserService.uploadAvatar(file);
        if (StringUtils.isBlank(avatarPath)){
            throw new UploadFileException("文件上传失败！");
        }
        return Response.okData(avatarPath);
    }

    /**
     * 更新头像
     * @param dto
     * @return
     */
    @PostMapping("update_avatar")
    @Log(module = "用户信息",operate = "更新头像")
    @ApiOperation(value = "更新头像",notes = "更新头像")
    public Response updateAvatar(@RequestBody @Validated UpdateAvatarDto dto){
        sysUserService.updateAvatar(dto);
        return Response.ok();
    }

    /**
     * 更新个人中心
     * @param dto
     * @return
     */
    @PostMapping("update_file")
    @Log(module = "用户信息",operate = "更新个人中心")
    @ApiOperation(value = "更新个人中心",notes = "更新个人中心数据")
    public Response updateProfile(@RequestBody @Validated UpdateProfileDto dto){
        boolean result = sysUserService.updateProfile(dto);
        if (!result){
            return Response.fail();
        }
        return Response.ok();
    }

    /**
     * 更新密码
     * @param dto
     * @return
     */
    @PostMapping("update_password")
    @Log(module = "用户信息",operate = "更新密码")
    @ApiOperation(value = "更新密码",notes = "个人中心更新密码")
    public Response updatePassword(@RequestBody @Validated UpdatePasswordDto dto){
        return sysUserService.updatePassword(dto);
    }

}
